package com.wingsoft.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wingsoft.common.exception.ServiceException;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.wingsoft.common.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import com.wingsoft.common.utils.bean.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wingsoft.work.mapper.UserVipMapper;
import com.wingsoft.work.domain.UserVip;
import com.wingsoft.work.service.IUserVipService;
import com.wingsoft.common.utils.IdGenerator.IdGeneratorFactory;
import com.wingsoft.work.dto.UserVipDto;
import com.wingsoft.work.vo.UserVipVo;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户套餐Service业务层处理
 *
 * @author hemj
 * @date 2025-10-20
 */
@Service
public class UserVipServiceImpl extends MPJBaseServiceImpl<UserVipMapper, UserVip> implements IUserVipService
{
    @Autowired
    private IdGeneratorFactory idGeneratorFactory;

    /**
         * 验证参数
         */
    private void validateData(UserVip userVip){
        if (ObjectUtils.isEmpty(userVip.getUserId())){
            throw new ServiceException("用户id不能为空");
        }
        if (ObjectUtils.isEmpty(userVip.getPointNum())){
            throw new ServiceException("积分不能为空");
        }
        if (ObjectUtils.isEmpty(userVip.getExpireTime())){
            throw new ServiceException("到期时间不能为空");
        }
    }

    /**
     * 保存数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public UserVip saveCascade(UserVipDto userVipDto){
        UserVip userVip = new UserVip();
        BeanUtils.copyProperties(userVipDto,userVip);
        validateData(userVip);
        userVip.setId(idGeneratorFactory.getId(UserVip.class.getSimpleName()));
        this.save(userVip);
        return userVip;
    }
    /**
     * 编辑数据
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public UserVip updateCascade(UserVipDto userVipDto){
        UserVip userVip = this.getById(userVipDto.getId());;
        BeanUtils.copyPropertiesIgnoreNull(userVipDto,userVip);
        validateData(userVip);
        this.updateById(userVip);
        return userVip;
    }

    /**
     * 初始化查询条件
     */
    public MPJLambdaWrapper<UserVip> initQueryWrapper(UserVipDto userVipDto){
        MPJLambdaWrapper<UserVip> queryWrapper = new MPJLambdaWrapper<>();
        if (!ObjectUtils.isEmpty(userVipDto.getUserId())) {
            queryWrapper.eq(UserVip::getUserId, userVipDto.getUserId());
        }
        if (!ObjectUtils.isEmpty(userVipDto.getPointNum())) {
            queryWrapper.eq(UserVip::getPointNum, userVipDto.getPointNum());
        }
        if (!ObjectUtils.isEmpty(userVipDto.getExpireTime())) {
            queryWrapper.eq(UserVip::getExpireTime, userVipDto.getExpireTime());
        }
        queryWrapper.orderByDesc(UserVip::getId);
        return queryWrapper;
    }

    /**
     * list查询
     */
    public List<UserVipVo> queryList(UserVipDto userVipDto) {
        List<UserVip> userVips = this.selectJoinList(UserVip.class,initQueryWrapper(userVipDto));
        List<UserVipVo> vos = new ArrayList<>();
        for (UserVip userVip:userVips) {
            UserVipVo userVipVo = new UserVipVo();
            BeanUtils.copyProperties(userVip,userVipVo);
            vos.add(userVipVo);
        }
        return vos;
    }

    /**
     * 统计
     */
    public long countList(UserVipDto userVipDto) {
        return this.count(initQueryWrapper(userVipDto));
    }

    /**
     * 获取单个数据
     */
    public UserVipVo getOneById(Long id){
        UserVip userVip = this.getById(id);
        UserVipVo userVipVo = new UserVipVo();
        BeanUtils.copyProperties(userVip,userVipVo);
        return userVipVo;
    }

    /**
     * 删除数据
     * @param ids
     */
    @Transactional(timeout = 3000, isolation = Isolation.READ_UNCOMMITTED, propagation = Propagation.REQUIRED)
    public void removeByIdList(List<Long> ids) {
        this.removeByIds(ids);
    }

    public UserVipVo isVip() {
        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<UserVip> vipWrapper = new LambdaQueryWrapper<>();
        vipWrapper.eq(UserVip::getUserId, userId);
        UserVip userVip = this.getOne(vipWrapper,false);
        if (ObjectUtils.isEmpty(userVip)) {
            UserVipVo userVipVo = new UserVipVo();
            userVipVo.setStatus(2);
            return userVipVo;
        }
        UserVipVo userVipVo = new UserVipVo();
        BeanUtils.copyProperties(userVip,userVipVo);
        return userVipVo;
    }

    public void backByIdList(List<Long> list) {
        LambdaUpdateWrapper<UserVip> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserVip::getPointNum, BigDecimal.ZERO);
        updateWrapper.in(UserVip::getId, list);
        this.update(updateWrapper);
    }

    public void backUid(UserVipDto userVipDto) {
        LambdaUpdateWrapper<UserVip> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(UserVip::getPointNum, BigDecimal.ZERO);
        updateWrapper.eq(UserVip::getUserId, userVipDto.getUserId());
        this.update(updateWrapper);
    }
}
